﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MassIntellect.WorkIt;

namespace MassIntellect.WorkIt
{
	public class ExceptionLog : BasicLog, IExceptionHandlerKeeper
	{
		#region IExceptionHandler Members

		private string GetExceptionDesc(Exception ex)
		{
			var sb = new StringBuilder();
			foreach (var item in ex.RecurseInner().Reverse())				
					sb.AppendFormat("{0} {1}",Environment.NewLine, item.Message);

			sb.AppendFormat("{0}StackTrace:{0}{1}",Environment.NewLine, ex.StackTrace);
			return sb.ToString();
		}

		public bool ExceptionHandler(IPipe sender, Exception ex)
		{
			LogNative("{{{0} [ERROR] {1}: {2}}}", GetDateTimePrefix(), sender.Name, GetExceptionDesc(ex));
			return true;
		}

		#endregion
	}
}
